package com.itqhy.interceptor;
import com.itqhy.pojo.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Interceptor1 implements HandlerInterceptor {
   //返回boolean值，返回true放行，返回false拦截
   //执行时间：controller方法没有被执行,ModelAndView没有被返回
   //使用场景：权限验证
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        User user= (User) request.getSession().getAttribute("user");
        //判断用户有没有登录
        if (user==null){
            request.getRequestDispatcher("/logincheck.jsp").forward(request,response);
            return false;
        }
        return true;

    }
    //返回boolean值，返回true放行，返回false拦截
    //执行时间：controller方法已经执行,ModelAndView没有被返回
    //使用场景：可以在此方法中设置全局的数据处理业务
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }
    //执行时间：controller方法已经执行,ModelAndView已经返回
    //使用场景：记录操作日志，记录登录用户的ip，时间等
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
    }
}
